package com.ltu.timkor6.gamesite.games.suduku.server;

import java.util.List;

import com.ltu.timkor6.gamesite.games.suduku.client.SudukuService;
import com.ltu.timkor6.gamesite.server.DataBaseConnection;
import com.ltu.timkor6.gamesite.server.GameSiteServiceImpl;
import com.ltu.timkor6.gamesite.shared.Result;

@SuppressWarnings("serial")
public class SudukuServiceImpl extends GameSiteServiceImpl implements SudukuService
{
	private Suduku suduku;
	private Result result;
			
	public SudukuServiceImpl()
	{
		this.result = new Result();
		this.result.setGameTitle("Suduku");
		
		this.suduku = new Suduku();
	}
	
	/**
	 * First time, starting game.
	 */
	public List<List<Integer>> start(int emptyCells)
	{
		//emptyCells = 41;
		emptyCells = 1;
		
		this.result.setTurns(emptyCells);
		
		this.suduku.generate();
		this.suduku.createGame(81-emptyCells);
		this.suduku.backtrace();
		
		//return null;
		return this.suduku.getGame();
	}
	
	/**
	 * Check an answer and return given points.
	 */
	public Result finished(List<List<Integer>> list)
	{
		if(this.suduku.equal(list))
		{
			int points = givePoints(100);
			this.result.setPoints(points);
			
			if(this.userInfo != null)
			{
				new DataBaseConnection(this).addResult(result);
			}
			
			return result;
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * Calculate points.
	 * @return
	 */
	private int givePoints(int moves)
	{
		int points = 0;
		
		// The more elements the bigger score
		points += this.result.getTurns() * 5;
		
		// The more extra moves the lesser score
		//points -= moves - this.minimumMoves;
		
		return points;
	}
}
